<?php

namespace app\model\withdraw;


use app\exception\IndexException;
use app\model\BaseModel;

class UserWithdrawAccountModel extends BaseModel
{
    protected $table = 'video_user_withdraw_account';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected static $instance;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function getDetail($where,$field=[])
    {
        $query = $this->where($where);
        $field && $detail = $query->select($field);
        $detail =  $query->first();

        if($detail==false){
            throw new IndexException(4020,'数据不存在');
        }

        return $detail;
    }

    public function saveData($param)
    {
        $info = $this->getWhere(['user_sn'=>$param['user_sn'],'isdel'=>0,'type'=>$param['type']])->first();
        if(!empty($info)){
            $data = [];
            !empty($param['mobile']) && $data['mobile'] = $param['mobile'];
            !empty($param['truename']) && $data['truename'] = $param['truename'];
            !empty($param['account']) && $data['account'] = $param['account'];
            !empty($param['idcard']) && $data['idcard'] = $param['idcard'];
            !empty($param['is_sign']) && $data['is_sign'] = $param['is_sign'];
            !empty($param['is_bind']) && $data['is_bind'] = $param['is_bind'];
            !empty($param['bankname']) && $data['bankname'] = $param['bankname'];

            !empty($param['isdel']) && $data['isdel'] = $param['isdel'];

            return $this->where(['id'=>$info['id']])->update($data);
        }else{
            $data = [];
            $data['user_sn'] = $param['user_sn']??request()->curUserId;
            $data['mobile'] = $param['mobile']??'';
            $data['truename'] = $param['truename']??'';
            $data['account'] = $param['account']??'';
            $data['idcard'] = $param['idcard']??'';
            $data['is_sign'] = $param['is_sign']??'0';
            $data['is_bind'] = $param['is_bind']??'1';
            $data['type'] = $param['type']??'1';
            $data['bankname'] = $param['bankname']??'';

            $id = $this->insertGetId($data);

            return $id;
        }
    }
}
